package com.lmy.product.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmy.common.utils.PageUtils;
import com.lmy.common.utils.Query;
import com.lmy.product.entity.AttrEntity;
import com.lmy.product.entity.AttrGroupEntity;
import com.lmy.product.mapper.AttrGroupDao;
import com.lmy.product.service.AttrGroupService;
import com.lmy.product.service.AttrService;
import com.lmy.vo.AttrGroupAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    /*    @Override
        public PageUtils queryPage(Map<String, Object> params) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );

            return new PageUtils(page);
        }*/
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupAttrsVo> getAttrGroupWithAttrs(Long catelogId) {
        //查询指定分组
        List<AttrGroupEntity> groupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>()
                        .eq("catelog_id", catelogId));
        //查询所有属性
        List<AttrGroupAttrsVo> attrsVos = groupEntities.stream().map(group -> {
            AttrGroupAttrsVo attrGroupAttrsVo = new AttrGroupAttrsVo();
            BeanUtils.copyProperties(group, attrGroupAttrsVo);
            List<AttrEntity> attrEntityList = attrService.attrRelation(attrGroupAttrsVo.getAttrGroupId());
            attrGroupAttrsVo.setAttrs(attrEntityList);
            return attrGroupAttrsVo;
        }).filter(item -> item.getAttrs() != null).collect(Collectors.toList());
        return attrsVos;
    }
}